﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.IO.Ports;
using TCA_User.Common;

namespace TCA_User.Setting
{
    public partial class ChangePass : SQL_APPL
    {
        UserInfo_struct My_Userinfo;
        private string TCA_Database_Conn = @"server=NGUYENTRUNGKIEN\SQLEXPRESS;database=TCA_DATABASE;uid=sa;pwd=123456";

        // khai báo 1 hàm delegate
        public delegate void GetInfo(UserInfo_struct User_Info);
        // khai báo 1 kiểu hàm delegate
        public GetInfo Update_Configure_Setting;

        public ChangePass(UserInfo_struct user_info)
        {
            InitializeComponent();
            ChangePass_HidePass.Checked = true;
            My_Userinfo = user_info;
            ChangePass_Username.Text = My_Userinfo.UserName;
            ChangePass_Username.Enabled = false;
        }

        private void ChangePass_HidePass_CheckedChanged(object sender, EventArgs e)
        {
            ChangePass_OldPass.UseSystemPasswordChar = ChangePass_HidePass.Checked;
            ChangePass_NewPass.UseSystemPasswordChar = ChangePass_HidePass.Checked;
            ChangePass_ConfirmPass.UseSystemPasswordChar = ChangePass_HidePass.Checked;
        }

        private void ChangePass_SaveBT_Click(object sender, EventArgs e)
        {
            if (My_Userinfo.Password.Trim() != ChangePass_OldPass.Text.Trim())
            {
                MessageBox.Show("Wrong Old Pass", "Error");
            }
            else
            {
                if (ChangePass_NewPass.Text.Trim() != ChangePass_ConfirmPass.Text.Trim())
                {
                    MessageBox.Show("Confirm Pass not match with New Pass", "Error");
                }
                else {
                    Update_New_Password();

                    Update_Configure_Bill_form();
                    
                }
            }
        }

        private void ChangePass_CancelBT_Click(object sender, EventArgs e)
        {
            ChangePass_OldPass.Text = "";
            ChangePass_NewPass.Text = "";
            ChangePass_ConfirmPass.Text = "";

        }

        private void ChangePass_ExitBT_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Update_New_Password()
        {
            string new_pass = ChangePass_NewPass.Text.Trim();
            string sql_cmd = @"UPDATE [Account_TBL] SET Password = '" + new_pass + "' WHERE UserName = '"
                               + ChangePass_Username.Text.Trim() + "'";

            if (Update_Data_Info(TCA_Database_Conn, sql_cmd) == true)
            {
                MessageBox.Show("Change Pass Success", "Success");
            }
            else
            {
                MessageBox.Show("Change Pass Failed", "Failed");
            }
        }

        private void Update_Configure_Bill_form()
        {
            My_Userinfo.Password = ChangePass_NewPass.Text.Trim();
            ChangePass_OldPass.Text = "";
            ChangePass_NewPass.Text = "";
            ChangePass_ConfirmPass.Text = "";

            if (Update_Configure_Setting != null)
            {
                Update_Configure_Setting(My_Userinfo);
            }

        }
    }
}
